package com.example.aigc_education.integration.processor.v1.async;

import com.example.aigc_education.enums.PaperDifficultyType;
import com.example.aigc_education.integration.emuns.CozeAsyncWorkflow;
import com.example.aigc_education.integration.processor.v1.CozeAsyncWFProcessor;
import com.example.aigc_education.integration.request.CozeRequest;
import com.example.aigc_education.integration.request.params.v1.CozeGenFlashCParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CozeAsyncGenFcProcessor extends CozeAsyncWFProcessor<CozeGenFlashCParam> {

    @Override
    public CozeAsyncWorkflow workflowType() {
        return CozeAsyncWorkflow.GEN_FC;
    }

    @Override
    protected String workflowId() {
        return CozeAsyncWorkflow.GEN_FC.getWorkflowId();
    }

    @Override
    protected boolean isParamValid(CozeRequest<CozeGenFlashCParam> request) {
        //参数校验
        CozeGenFlashCParam parameters = request.getParameters();
        if (parameters == null) {
            log.error("参数为空");
            return false;
        }
        // 校验 topic
        if (parameters.getTopic() == null || parameters.getTopic().trim().isEmpty()) {
            log.error("主题不能为空");
            return false;
        }

        // 校验 subtopic，如果存在则进行校验
        if (parameters.getSubtopic() != null && parameters.getSubtopic().trim().isEmpty()) {
            log.error("子主题不能为空");
            return false;
        }

        // 校验 cardNum
        try {
            int cardNum = Integer.parseInt(parameters.getCardNum());
            if (cardNum <= 0) {
                log.error("卡片数量必须是正整数");
                return false;
            }
        } catch (NumberFormatException e) {
            log.error("卡片数量格式不正确", e);
            return false;
        }

        // 校验 difficulty
        try {
            PaperDifficultyType.fromTitle(parameters.getDifficulty());
        } catch (IllegalArgumentException e) {
            log.error("难度值无效", e);
            return false;
        }
        return true;
    }

    @Override
    protected CozeRequest<CozeGenFlashCParam> buildRequest(CozeAsyncPlanRequest request) {
        CozeGenFlashCParam param = new CozeGenFlashCParam();
        param.setTopic(request.getTopic());
        param.setSubtopic(request.getSubtopic());
        // default
        param.setDifficulty("easy");
        param.setCardNum("10");
        CozeRequest<CozeGenFlashCParam> cozeRequest = new CozeRequest<>();
        cozeRequest.setParameters(param);
        return cozeRequest;
    }
}
